package com.qingfeng.client.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingfeng.client.DO.GoodsAttrsVo;
import com.qingfeng.client.DO.GoodsDetailAllInfo;
import com.qingfeng.client.DO.GoodsDetailDo;
import com.qingfeng.client.VO.GoodsConditionVo;
import com.qingfeng.client.bean.Goods2attrs;
import com.qingfeng.client.bean.GoodsCommon;
import com.qingfeng.client.bean.Store;
import com.qingfeng.client.dao.Goods2attrsMapper;
import com.qingfeng.client.dao.GoodsCommonMapper;
import com.qingfeng.client.dao.GoodsMapper;
import com.qingfeng.client.dao.StoreMapper;
import com.qingfeng.client.util.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.OptionalInt;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/3/9 9:07 上午
 */
@Service
public class GoodsService{

    @Resource
    private GoodsCommonMapper goodsCommonMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private Goods2attrsMapper goods2attrsMapper;


    public IPage getPage(Page page,  GoodsConditionVo condition ){
       return goodsCommonMapper.getPage(page,condition);
    }

    /**
     * 获取商品详情
     * @param goodsCommonId
     * @return
     */
    public GoodsDetailAllInfo detail(long goodsCommonId){
        GoodsCommon goodsCommon = goodsCommonMapper.selectById(goodsCommonId);
        List<GoodsDetailDo> goods = goodsMapper.getDetail(goodsCommonId);
        int minPrice = 0;
        int maxPrice = 0;
        List<GoodsAttrsVo> attrsList = goods2attrsMapper.getByGoodsCommonId(goodsCommonId);
        for (GoodsDetailDo goodsDetailDo:goods){
            if (goodsDetailDo.getValueIds()==null) continue;
            String[] attrs = goodsDetailDo.getValueIds().split(",");
            int price = goodsDetailDo.getPrice();
            if (minPrice>price || minPrice==0){
                minPrice=price;
            }
            if (maxPrice<price){
                maxPrice=price;
            }

            if (attrs.length>0){
                goodsDetailDo.setAttr1(attrs[0]);
            }
            if (attrs.length>1){
                goodsDetailDo.setAttr2(attrs[1]);
            }
            if (attrs.length>2){
                goodsDetailDo.setAttr3(attrs[2]);
            }
            if (attrs.length>3){
                goodsDetailDo.setAttr4(attrs[3]);
            }
            if (attrs.length>4){
                goodsDetailDo.setAttr5(attrs[4]);
            }
            if (attrs.length>5){
                goodsDetailDo.setAttr6(attrs[5]);
            }
            goodsDetailDo.setValueIds(null);
        }
        goodsCommon.setPrice(minPrice);
        goodsCommon.setMaxPrice(maxPrice);
        GoodsDetailAllInfo goodsDetailAllInfo = new GoodsDetailAllInfo();
        goodsDetailAllInfo.setGoodsCommon(goodsCommon);
        goodsDetailAllInfo.setGoodsDetailDos(goods);
        goodsDetailAllInfo.setAttrs(attrsList);
        return goodsDetailAllInfo;
    }




}
